//
//  QuestionConsultation.m
//  YZZX
//
//  Created by Easaa on 15/6/11.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "QuestionConsultation.h"

@interface QuestionConsultation ()<UITextFieldDelegate,UITextViewDelegate,HXHttpRequestDelegate>
{
    UITextField *OldPassWord;
    UITextView  *MassgeTextView;
    UILabel  *lableplacel;
    HXHttpRequest *_httpRequest;
    
}

@end

@implementation QuestionConsultation

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"提问咨询";
    self.view.backgroundColor =RGBCOLOR(230, 230, 230);
    
    
    OldPassWord =[[UITextField alloc]initWithFrame:CGRectMake(0, 15, Dev_ScreenWidth, 40)];
    OldPassWord.tag =0;
    OldPassWord.delegate =self;
    [OldPassWord.layer setMasksToBounds:YES];
   // [OldPassWord.layer setCornerRadius:3]; //设置矩圆角半径
    //[OldPassWord.layer setBorderWidth:1.0];   //边框宽度
   // CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0.5 });
   // [OldPassWord.layer setBorderColor:colorref];//边框颜色
    OldPassWord.backgroundColor = [UIColor whiteColor];
    OldPassWord.placeholder = @"请输入问题标题...";
    OldPassWord.font = [UIFont systemFontOfSize:16];//[UIFont fontWithName:@"Arial" size:20.0f];
    [self.view addSubview:OldPassWord];
    

    
    MassgeTextView =[[UITextView alloc]initWithFrame:CGRectMake(0,OldPassWord.bottom+15, Dev_ScreenWidth, 90)];
    
    MassgeTextView.backgroundColor =[UIColor whiteColor];
    MassgeTextView.tag =3;
    MassgeTextView.delegate =self;
    //[MassgeTextView.layer setMasksToBounds:YES];
    //[MassgeTextView.layer setCornerRadius:3]; //设置矩圆角半径
    //[MassgeTextView.layer setBorderWidth:0.5];   //边框宽度
   // CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    //CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){0, 0, 0, 0.5});
    
    //[MassgeTextView.layer setBorderColor:colorref];//边框颜色
    //_MassgeTextView.backgroundColor = [UIColor redColor];
    [self.view addSubview:MassgeTextView];
    
    //焦点
    [OldPassWord becomeFirstResponder];
    
    lableplacel =[[UILabel alloc]init];
    lableplacel.frame =CGRectMake(2,0, Dev_ScreenWidth, 30);
    lableplacel.text = @"请叙述问题详情...";
    lableplacel.textColor =RGBCOLOR(250, 250, 250);
    lableplacel.enabled = NO;//lable必须设置为不可用
    lableplacel.backgroundColor = [UIColor clearColor];
    lableplacel.font =[UIFont systemFontOfSize:16];
    [MassgeTextView addSubview:lableplacel];
    

    
    
    UIButton  *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomButton.tag = 2015 ;
    bottomButton.frame =CGRectMake(10,MassgeTextView.bottom+40 ,Dev_ScreenWidth-20, 40);
    [bottomButton addTarget:self action:@selector(requestHeadlDataFantion) forControlEvents:UIControlEventTouchUpInside];
    [bottomButton.layer setMasksToBounds:YES];
    [bottomButton.layer setCornerRadius:2]; //设置矩圆角半径
    [bottomButton.layer setBorderWidth:0];   //边框宽度
    [bottomButton setTitle:@"提问" forState:UIControlStateNormal];
    [bottomButton setTintColor:[UIColor whiteColor]];
    bottomButton.backgroundColor =RGBCOLOR(252, 78, 125);
    [self.view addSubview:bottomButton];

    
    
}


- (void)requestHeadlDataFantion
{
  //  [OldPassWord resignFirstResponder];
   // [MassgeTextView resignFirstResponder];
    
    if(OldPassWord.text.length<=0 || MassgeTextView.text.length<=0)
    {
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入完整！"                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1028;
        [AlertView show];
        return;
        
    }
    
    
    
    
    NSDictionary *Dickey = [NSDictionary dictionaryWithObjectsAndKeys:
                            OldPassWord.text,    @"title",
                            MassgeTextView.text,   @"content",
                            nil];
    
    //列表请求
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString],    @"user_id",
                               @"1",                @"site_id",
                               [NSNumber numberWithInteger:1],                @"consultant_id",
                               @"articles",         @"app",
                               @"article",          @"model",
                               [NSString stringWithFormat:@"%@",self.id_massge]  ,      @"object_id",
                               Dickey,              @"post_data",
                               nil];
    
    
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_COUNSEL_CONSULTING_SEND andTag:1001];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
//    
//    {
//        "user_id": "1",
//        "site_id": "1",
//        "consultant_id": {{ consultant_id }},
//        "app": "{{ app }}",
//        "model": "{{ model }}",
//        "object_id": "{{ object_id }}",
//        "post_data":
//        {
//            "title": "{{ title }}",
//            "content": "{{ content }}"
//        }
//    }
}



-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        lableplacel.text = @"请叙述问题详情...";
    }else{
        lableplacel.text = @"";
    }
}


- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    [theTextField resignFirstResponder];
    
    return YES;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder]; return NO;
    }
    return YES;
}



#pragma mark alertView 弹框代理
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     [self.view endEditing:YES];
    if(alertView.tag == 1020)
    {
        if(buttonIndex == 0)
        {
            [self.navigationController popViewControllerAnimated:YES];
            [self.tableviewsdegat creatReaqstUrlDataView];
  
        }
    }if(alertView.tag == 1025)
    {
        OldPassWord.text = @"";
        MassgeTextView.text = @"";
     
    }
    
}




#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    //tableview列表数据
    if(tag == 1001)
    {
        //  NSLog(@"---------------->>>> %@",responseString);
        
        NSDictionary *dic = [[NSDictionary alloc]init];
        dic = [JsonUtil jsonToDic:responseString];
        
        // NSLog(@"---------------->>>> \n%@",dic);
        
        
        if([[dic objectForKey:@"is_success"] boolValue])
        {
            UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                                message:[dic objectForKey:@"message"]
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
            
            AlertView.tag = 1020;
            [AlertView show];
            
        }else
        {
            UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                                message:[dic objectForKey:@"message"]
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
            
            AlertView.tag = 1025;
            [AlertView show];
            
        }
        
    }
    
    
}

- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    
    
    
    
}




@end
